﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class TestScript : MonoBehaviour
{
    public TextAsset navMeshTextAsset;
    public Transform startTransform;
    public Transform endTransform;

    private int mCurrentNavMeshId;
    private List<Vector3> mPathPointList = new List<Vector3>();

    void Start()
    {
        mCurrentNavMeshId = NavMeshInterface.LoadFromFile("Assets/Resources/srv_CAIBakedNavmesh.bytes");
        //mCurrentNavMeshId = NavMeshInterface.LoadFromData(navMeshTextAsset.bytes, navMeshTextAsset.bytes.Length);

    }

    void OnDestroy()
    {
        ReleaseTest();
    }

    public void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        DrawGizmosLine(mPathPointList);
    }

    private void DrawGizmosLine(List<Vector3> linePointList)
    {
        int lineCount = linePointList.Count - 1;
        for (int i = 0; i < lineCount; ++i)
        {
            Gizmos.DrawLine(linePointList[i], linePointList[i + 1]);
        }
    }

    private void Update()
    {
        //FindStraightPathTest();
        //GetPolyHeightTest();
        //FindNearestPolyTest();
        //IsWalkableTest();
        //RaycastTest();
        //MoveAlongSurfaceTest();
    }

    public void FindStraightPathTest()
    {
        mPathPointList.Clear();

        Vector3 startPoint = startTransform.position;
        Vector3 endPoint = endTransform.position;
        float[] resultPath = new float[256];
        int pathCount = NavMeshInterface.FindStraightPath(mCurrentNavMeshId, startPoint.x, startPoint.y, startPoint.z, endPoint.x, endPoint.y, endPoint.z, resultPath, resultPath.Length);
        for (int i = 0; i < pathCount; ++i)
        {
            Vector3 pathPoint = new Vector3();
            pathPoint.x = resultPath[i * 3];
            pathPoint.y = resultPath[i * 3 + 1];
            pathPoint.z = resultPath[i * 3 + 2];

            mPathPointList.Add(pathPoint);
        }
    }

    public void GetPolyHeightTest()
    {
        Vector3 startPoint = startTransform.position;
        float height = 0.0f;
        if (NavMeshInterface.GetPolyHeight(mCurrentNavMeshId, startPoint.x, startPoint.y, startPoint.z, ref height))
        {
            Debug.Log(height);
        }
        else
        {
            Debug.Log("GetPolyHeightTest Failed");
        }
    }

    public void FindNearestPolyTest()
    {
        Vector3 startPoint = startTransform.position;
        float[] nearestPoint = new float[3];
        if (NavMeshInterface.FindNearestPoly(mCurrentNavMeshId, startPoint.x, startPoint.y, startPoint.z, false, nearestPoint))
        {
            Debug.Log(nearestPoint);
        }
    }

    public void RaycastTest()
    {
        Vector3 startPoint = startTransform.position;
        Vector3 endPoint = endTransform.position;
        if (NavMeshInterface.Raycast(mCurrentNavMeshId, startPoint.x, startPoint.y, startPoint.z, endPoint.x, endPoint.y, endPoint.z))
        {
            mPathPointList.Clear();

            mPathPointList.Add(startPoint);
            mPathPointList.Add(endPoint);
        }
    }

    public void MoveAlongSurfaceTest()
    {
        Vector3 startPoint = startTransform.position;
        Vector3 endPoint = endTransform.position;
        float[] resultPoint = new float[3];
        if (NavMeshInterface.MoveAlongSurface(mCurrentNavMeshId, startPoint.x, startPoint.y, startPoint.z, endPoint.x, endPoint.y, endPoint.z, resultPoint))
        {
            Debug.Log(resultPoint[0] + " " + resultPoint[1] + " " + resultPoint[2]);
        }
    }

    public void ReleaseTest()
    {
        NavMeshInterface.Release(mCurrentNavMeshId);
    }

    public void IsWalkableTest()
    {
        if (NavMeshInterface.IsWalkable(mCurrentNavMeshId, startTransform.position))
        {
            Debug.Log("walkable");
        }
        else
        {
            Debug.Log("not walkable");
        }
    }
}
